<?php

namespace app\admin\controller;

use app\admin\logic\BranchLogic;
use app\admin\logic\ClientLogic;
use app\admin\logic\CarLogic;
use app\admin\logic\ServerLogic;
use app\admin\logic\WorkLogic;

class Invoice extends Base
{
    // 打印单页
    public function index($id = 0)
    {
        $workLogic = new WorkLogic();
        // 已经填过的信息
        $work = $workLogic->findOne($id);
        $this->assign('work', $work);
        // p($work);
        // 检查结果数据
        $checks = json_decode($work['check_result'], true);
        // p($checks);
        $this->assign('checks', $checks);

        $serverLogic = new ServerLogic();
        // 服务历史
        $serverHistory = $serverLogic->getServerHistory($work['car_num']);
        // p($serverHistory);
        $this->assign('serverHistory', $serverHistory);

        $branchLogic = new BranchLogic();
        $branch = $branchLogic->findByWorkId($id);
        $address = explode("\n", $branch["address"]);
        $address1 = "";
        $address2 = "";
        if (count($address) > 1) {
            $address1 = $address[0];
            $address2 = $address[1];
        }
        $this->assign('branch', ["num" => $branch["num"], "address1" => $address1, "address2" => $address2, "phone" => $branch["phone"]]);

        //服务项目数据
        $servers = $serverLogic->getServerItems($id);
        $this->assign('server', $servers['rows']);
        return $this->fetch();
    }

    // 打印单页
    public function invoice()
    {
        return $this->fetch();
    }

    //添加客户信息
    public function addClient()
    {
        echo $this->fetch();
    }

    //保存客户信息
    public function doAddClient($param = '')
    {
        $data = json_decode($param, true);
        $data['inserttime'] = time();
        $data['lastupdatettime'] = time();

        $clientLogic = new ClientLogic();
        $res = $clientLogic->add($data);
        return ['res' => $res, 'data' => $data];
    }

    //选择客户信息
    public function chooseClient()
    {
        echo $this->fetch();
    }

    public function getClients($page = 1, $rows = 10)
    {
        $clientLogic = new ClientLogic();
        $data = $clientLogic->queryJqGrid('', $page, $rows);
        return $data;
    }


    //添加车辆信息
    public function addCar()
    {
        echo $this->fetch();
    }

    //保存车辆信息
    public function doAddCar($param = '')
    {
        $data = json_decode($param, true);
        $data['inserttime'] = time();
        $data['lastupdatettime'] = time();

        $carLogic = new CarLogic();
        $res = $carLogic->add($data);
        return ['res' => $res, 'data' => $data];
    }

    //选择车辆信息
    public function chooseCar()
    {
        echo $this->fetch();
    }

    public function getCars($page = 1, $rows = 10)
    {
        $vehicleLogic = new CarLogic();
        $data = $vehicleLogic->queryJqGrid('', $page, $rows);
        return $data;
    }

    // 接待人员打开出单页面
    public function indexInvoice()
    {
        return $this->fetch();
    }

    // 待付款工单界面
    public function pay()
    {
        return $this->fetch();
    }


}
